package com.jojo.android.zxlib.tool;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import java.util.HashMap;
import java.util.Map;


public class Global {

    private static Global instance;

    private Context context;
    private Handler mainHandler;
    private ZxThreadPoolTool threadPoolTool;
    private Map<String, Object> cache;


    private Global(Context context) {
        this.context = context;
        mainHandler = new Handler(Looper.getMainLooper());
        threadPoolTool = new ZxThreadPoolTool(ZxThreadPoolTool.Type.FixedThread, 5);
        cache = new HashMap<>();
    }

    public static void init(Context context) {
        instance = new Global(context);
    }

    public static Global getInstance() {
        if (instance == null) {
            throw new RuntimeException("Global no init()!!!!!");
        }
        return instance;
    }


    public Context getContext() {
        return context;
    }


    public void Exe(Runnable run) {
        threadPoolTool.execute(run);
    }


    public Handler getMainHandler() {
        return mainHandler;
    }

    public Object getCache(String key) {
        if (cache != null) {
            return cache.get(key);
        }
        return null;
    }

    public void putCache(String key,Object v){
        if (cache!=null){
            cache.put(key, v);
        }
    }

}
